package com.tedu.aopdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect// 标识当前类为切面类
public class SyssoutAspect {
    /*@Before("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public void before(JoinPoint joinPoint){
        *//*
             JoinPoint 切入点

         *//*
        *//*System.out.println("target:"+joinPoint.getTarget());
        System.out.println(joinPoint.getSignature().getName());
        System.out.println(joinPoint.getSignature());
        System.out.println("参数："+ Arrays.toString(joinPoint.getArgs()) );*//*
        // 切入点方法
        String methodName = joinPoint.getSignature().getName();
        // 切入点类名
        String className = joinPoint.getTarget().getClass().getSimpleName();
        // 切入点参数
        String args = Arrays.toString(joinPoint.getArgs());
        System.out.println("开始调用"+className+"."+methodName+"("+args+")");
    }


    @AfterReturning(
            value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
            returning = "returnValue"
            )
    public void afterReturning(JoinPoint joinPoint, Object returnValue){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()执行完毕，返回值为："+returnValue);
    }

    @After("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public void after(JoinPoint joinPoint){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()执行完毕");
    }

    @AfterThrowing(
            value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
            throwing = "e"
    )
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(className+"."+methodName+"()抛出异常："+e.getMessage());
    }*/

    @Around("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        String args = Arrays.toString(joinPoint.getArgs());
        // @Before 相当于 前置通知
        System.out.println("开始调用"+className+"."+methodName+"("+args+")");

        try {
            Object returnValue = joinPoint.proceed();// 执行目标方法
            // @AfterReturning 相当于 后置通知
            System.out.println(className+"."+methodName+"()执行完毕，返回值为："+returnValue);
            return returnValue;
        } catch (Throwable e) {
            // @AfterThrowing 相当于 异常通知
            System.out.println(className+"."+methodName+"()抛出异常："+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            // @After 相当于 最终通知
            System.out.println(className+"."+methodName+"()执行完毕");
        }
    }



}
